package com.cwt_taxi_xa.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.cwt_taxi_xa.ActivityACC;
import com.cwt_taxi_xa.ActivityFM;
import com.cwt_taxi_xa.ActivityMain;
import com.cwt_taxi_xa.ActivityWaitQD;
import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.ConstantsAram;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.record.RecordTimer;
import com.cwt_taxi_xa.record.RecorderBiz;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.timer.TimerHeartBeat;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.GPIOUtil;
import com.cwt_taxi_xa.util.SPUtils;

import java.util.Set;
import java.util.Timer;

public class KeyBroadcast extends BroadcastReceiver {

	Timer timer_bj;
	public static boolean baojing = false;
	static int bj_count = 0;
	static long bj_time = 0;

	public static boolean sendBJ = false;

	static long bs_bj_down = 0;

	@Override
	public void onReceive(Context arg0, Intent arg1) {
		// TODO Auto-generated method stub

		if (!ActivityACC.accon) {
			return;
		}

		Bundle b = arg1.getExtras();
		Set<String> ss = b.keySet();

		for (String str : ss) {
			AppLog.e("KeyBroadcast----" + str + ":" + b.getInt(str));
		}

		 if (b.getInt("type") == 3 && b.getInt("state") == 2) {
            //模式按键
            MCUUtil.mPartyControl.onChangeMode();
        }else if (b.getInt("type") == 2 && b.getInt("state") == 2) {
			//Home按键
			if (SPUtils.getParamBoolean(SPConstants.SPK_signin, SPConstants.SPV_signin)) {
				Log.e("ddd", "--------点击主页，当前已签到，打开main----------");
				startActivity(arg0, ActivityMain.class);
			} else {
				Log.e("ddd", "--------点击主页，当前未签到，打开waiter----------");
				startActivity(arg0, ActivityWaitQD.class);
			}
		} else if (b.getInt("type") == 10 && b.getInt("state") == 2) {
			//音量-
			MCUUtil.setvolLess();
		} else if (b.getInt("type") == 1 && b.getInt("state") == 2) {
			//音量+
			//MCUUtil.setvolAdd();
			//TTsFunction.speakText("不满意");//调试E5不满意
		} else if (b.getInt("type") == 5 && b.getInt("state") == 2) {
			//TTsFunction.speakText("FM");
			//TTsFunction.speakText("非常满意");
			// 视频
			if(!ActivityFM.isview){
				//startActivity(arg0, ActivityFM.class);//调试E5非常满意
			}
//			startActivity(arg0, VideoActivity.class);
		} else if (b.getInt("type") == 6 && b.getInt("state") == 2) {
			// 电召
			//TTsFunction.speakText("抱歉，电召功能暂时未开发");//调试E5一般
			//TTsFunction.speakText("一般");//调试E5一般
//			if(MyStatus.vichl!=3){
//				MyStatus.lastvichl = MyStatus.vichl;
//				MCUUtil.selecterVic(3);
//			}else{
//				MCUUtil.selecterVic(MyStatus.lastvichl);
//			}

		} else if (b.getInt("type") == 9 && b.getInt("state") == 2) {
			// 主页
			if (SPUtils.getParamBoolean(SPConstants.SPK_signin, SPConstants.SPV_signin)) {
				Log.e("ddd", "--------点击主页，当前已签到，打开main----------");
				startActivity(arg0, ActivityMain.class);
			} else {
				Log.e("ddd", "--------点击主页，当前未签到，打开waiter----------");
				startActivity(arg0, ActivityWaitQD.class);
			}
		}

		else if(b.getInt("type") == 8 && b.getInt("state") == 2){
			bs_bj_down = System.currentTimeMillis();
			new Thread(new Runnable() {
				@Override
				public void run() {
					long thisbs = bs_bj_down;
					try {
						Thread.sleep(300);
						if(thisbs==bs_bj_down&&GPIOUtil.readIOState("/proc/detect_warning") == 0){
							Thread.sleep(3000);
							if(thisbs == bs_bj_down&&GPIOUtil.readIOState("/proc/detect_warning") == 0){
//								if(baojing){
//									//取消报警
//									DDUtil.sendText2TT(6);
////									ConstantsAram.setAlarmStateValue(0, 0);
//									ConstantsAram.setAlarmStateValue(1, 0);
//									baojing = false;
//									RecordTimer.recordReason = 0;
//									// 停止录音
//									if (RecorderBiz.getRecordType() == 2) {
//										if (MyStatus.zhongche) {
//											// 重车,继续录音
//											RecordTimer.mediaIdList = new ArrayList<String>();
//										} else {
//											// 空车，结束录音
//											SerialPortService.handler.obtainMessage(1008).sendToTarget();
//										}
//									}
//								}else{
								//报警
								ConstantsAram.setAlarmStateValue(1, 1);
								MainApplication.topActivity.sendpic(3);
								baojing = true;
								TimerHeartBeat.baojingfasong = true;
								// 录音
								RecordTimer.recordReason = 2;
								RecordTimer.mediaIdList = null;
								if (RecorderBiz.getRecordType() == 2) {
									SerialPortService.handler.obtainMessage(1005).sendToTarget();
								} else {
									RecorderBiz recorderBiz = new RecorderBiz(MainApplication.getInstance());
									recorderBiz.updateReason(RecordTimer.recordReason);
								}
//								}
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}).start();
		}


//		else if (b.getInt("type") == 8 && b.getInt("state") == 2) {
//			// GPIO状态变化
//
//			// 启动报警按钮检测
////			if (bj_count == 0) {
////				bj_count++;
////				bj_time = System.currentTimeMillis();
////			} else if (bj_count == 1) {
////				if (System.currentTimeMillis() - bj_time > 1000) {
////					bj_count = 0;
////					bj_time = 0;
////					AppLog.e("0----------------------------报警");
////					// SerialPortService.getInstance().sendSerialData1(DDUtil.getDDstatue(6));
////					DDUtil.sendText2TT(2);
////					ConstantsAram.setAlarmStateValue(0, 1);
////					ConstantsAram.setAlarmStateValue(1, 1);
////					baojing = true;
////					TimerHeartBeat.baojingfasong = true;
////					// 录音
////					RecordTimer.recordReason = 2;
////					RecordTimer.mediaIdList = null;
////					if (RecorderBiz.getRecordType() == 2) {
////						SerialPortService.handler.obtainMessage(1005).sendToTarget();
////					} else {
////						RecorderBiz recorderBiz = new RecorderBiz(MainApplication.getInstance());
////						recorderBiz.updateReason(RecordTimer.recordReason);
////					}
////				} else {
////					bj_count++;
////				}
////			} else if (bj_count == 2) {
////				bj_count++;
////			} else {
////				bj_count = 0;
////				bj_time = 0;
////				AppLog.e("0----------------------------解除报警");
////				// SerialPortService.getInstance().sendSerialData1(DDUtil.getDDstatue(7));
////				// SerialPortService.getInstance().sendSerialData1(DDUtil.getDDstatue(7));
////				DDUtil.sendText2TT(6);
////				ConstantsAram.setAlarmStateValue(0, 0);
////				ConstantsAram.setAlarmStateValue(1, 0);
////				baojing = false;
////				RecordTimer.recordReason = 0;
////				// 停止录音
////				if (RecorderBiz.getRecordType() == 2) {
////					if (MyStatus.zhongche) {
////						// 重车,继续录音
////						RecordTimer.mediaIdList = new ArrayList<String>();
////					} else {
////						// 空车，结束录音
////						SerialPortService.handler.obtainMessage(1008).sendToTarget();
////					}
////				}
////			}
//
//		}

		/**
		 *
		 * 6 4 1 2 3
		 *
		 */

	}

	public void startActivity(Context context, Class class1) {
		Intent intent2 = new Intent();
		intent2.setClass(context, class1);
		intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 这一行非常关键，如果不加这个，就不能在这实现Activity的启动。
		context.startActivity(intent2);
	}

}
